package com.yanxu.base;

import com.utils.Const;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

/**
 * @author 折戟沉沙铁未销
 * @version V1.0
 * @date 2024/3/31-2024
 * @Description: 这里用一句话描述这个类的作用
 */
public class BaseProducer2 {
    public static void main(String[] args) throws Exception {
        //定义生产者，生产者组名字为 'b'
        DefaultMQProducer producer = new DefaultMQProducer("b");
        producer.setNamesrvAddr(Const.NAME_SERVER_SINGLE);

        //设置队列的个数
        producer.setDefaultTopicQueueNums(4);

        //启动 producer
        producer.start();

        //发送消息到 【topic-a】 主题上
        for (int i = 0; i < 10; i++) {
            Message message = new Message("imooc-topic-a", "tag-a-" + i, "key-a-" + i, ("消息a" + i).getBytes());
            //message.setKeys(UUID.randomUUID());
            message.setDelayTimeLevel(3);
            SendResult send = producer.send(message);
            System.out.println(send.getSendStatus());
            /*SendResult send = producer.send(message, new MessageQueueSelector() {
                @Override
                public MessageQueue select(List<MessageQueue> list, Message message, Object o) {
                    System.out.println("first:" + list.size());
                    int size = list.size();
                    return list.get(size-1);
                }
            },null);*/
        }

        //发送消息到  【topic-b】主题上
        for (int i = 0; i < 10; i++) {
            Message message = new Message("imooc-topic-b", "tag-b-" + i, "key-b-" + i, ("消息b" + i).getBytes());
            message.setDelayTimeLevel(4);
            SendResult send = producer.send(message);
            System.out.println(send.getSendStatus());
            /*SendResult send = producer.send(message, new MessageQueueSelector() {
                @Override
                public MessageQueue select(List<MessageQueue> list, Message message, Object o) {
                    System.out.println("second:" + list.size());
                    return list.get(1);
                }
            },null);*/
        }

        System.out.println("发送结束了 !!!");
        producer.shutdown();
    }
}